
package com.kamikaze.lamg;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import java.util.ArrayList;

public class MovieImageActivity extends Activity {

    private ArrayList<Bitmap> movieImages;

    private ViewPager pager;

    MovieDBHelper dbHelper;

    private Button btnPrev;

    private Button btnNext;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.image_main);

        dbHelper = new MovieDBHelper(this);
        MovieInfo movieInfo = (MovieInfo) getIntent().getSerializableExtra("movieInfo");

        btnPrev = (Button) findViewById(R.id.btnPrev);
        btnNext = (Button) findViewById(R.id.btnNext);

        movieImages = dbHelper.getMovieImages(movieInfo.id);
        if (movieImages.size() == 0) {
            finish();
            return;
        }
        pager = (ViewPager) findViewById(R.id.pager);
        pager.setAdapter(new MoviePagerAdapter(movieImages, this));
        pager.setCurrentItem(0);
        pager.setOnPageChangeListener(new OnPageChangeListener() {

            @Override
            public void onPageSelected(int arg0) {
                setNextPrevVisibility(arg0);
            }

            @Override
            public void onPageScrolled(int arg0, float arg1, int arg2) {

            }

            @Override
            public void onPageScrollStateChanged(int arg0) {

            }
        });
    }

    @Override
    protected void onDestroy() {
        dbHelper.closeDatabase();
        movieImages.clear();
        movieImages = null;
        super.onDestroy();
    }

    private void setNextPrevVisibility(int position) {
        if (position <= 0) {
            btnPrev.setVisibility(View.INVISIBLE);
        } else {
            btnPrev.setVisibility(View.VISIBLE);
        }
        if (position >= movieImages.size() - 1) {
            btnNext.setVisibility(View.INVISIBLE);
        } else {
            btnNext.setVisibility(View.VISIBLE);
        }

    }

    public class MoviePagerAdapter extends PagerAdapter {
        private ArrayList<Bitmap> mImages;

        private LayoutInflater inflater;

        private ImageView imageView;

        MoviePagerAdapter(ArrayList<Bitmap> images, Context context) {
            mImages = images;

            inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {
            return movieImages.size();
        }

        @Override
        public boolean isViewFromObject(View arg0, Object arg1) {
            return arg0 == (arg1);
        }

        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            View view = inflater.inflate(R.layout.image_item, null);
            imageView = (ImageView) view.findViewById(R.id.movieImage);
            imageView.setImageBitmap(mImages.get(position));
            container.addView(view);
            return view;
        }

        @Override
        public void destroyItem(View collection, int position, Object view) {
            ((ViewPager) collection).removeView((View) view);
        }

    }
}
